//
//  UIButton+CXBtn.m
//  智积分
//
//  Created by apple on 2018/4/13.
//  Copyright © 2018年 Cynic. All rights reserved.
//

#import "UIButton+CXBtn.h"

@implementation UIButton (CXBtn)
static void *cx_btneventsBlock;
static void *namelableSpace;


-(BtnEventsBlock)CX_BtnEventsBlock{
    return objc_getAssociatedObject(self,&cx_btneventsBlock);
}

-(void)setCX_BtnEventsBlock:(BtnEventsBlock)CX_BtnEventsBlock{
    objc_setAssociatedObject(self, &cx_btneventsBlock, CX_BtnEventsBlock,OBJC_ASSOCIATION_COPY);
}


-(void)cx_eventsBlockHandler:(void (^)(void))block forControlEvents:(UIControlEvents)controlEvents{
    self.CX_BtnEventsBlock = block;
    [self addTarget:self action:@selector(cx_blockButtonClick) forControlEvents:controlEvents];
}

-(void)cx_blockButtonClick{
    if (self.CX_BtnEventsBlock) {
        self.CX_BtnEventsBlock();
    }
}


#pragma mark--拓展namelabel，方便btn上有多个label显示的情况
-(UITextField *)namelabel{
    return objc_getAssociatedObject(self, &namelableSpace);
}

#pragma mark--uibutton上添加固定尺寸image
-(void)LoadNamelableWithFrame:(CGRect)frame withFont:(NSInteger)fontsize withTextColor:(UIColor *)hexColor withEditEnabel:(BOOL)editEnabel WithtextAli:(NSTextAlignment)textali{
    if (!self.namelabel) {
        self.namelabel = [[UITextField alloc] initWithFrame:frame];
        [self addSubview:self.namelabel];
    }
    else{
        self.namelabel.frame = frame;
        self.namelabel.hidden = NO;
    }
    self.namelabel.userInteractionEnabled = editEnabel;
    self.namelabel.textColor = hexColor;
    self.namelabel.textAlignment = textali;
    self.namelabel.font = [UIFont systemFontOfSize:fontsize];
    
}

-(void)setNamelabel:(UITextField *)namelabel{
    namelabel.userInteractionEnabled = NO;
    objc_setAssociatedObject(self, &namelableSpace, namelabel, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

//-(instancetype)init{
//    self = [super init];
//    self.namelabel.userInteractionEnabled = NO;
//    return self;
//}
@end
